package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ...
	 * @author vincent
	 */
	public class Preloader extends MovieClip 
	{
		[Embed(source="../assets/dotBg.png")]
		private var DotBg:Class;
		
		
		public function Preloader() 
		{
			if (stage) {
				stage.scaleMode = StageScaleMode.NO_SCALE;
				stage.align = StageAlign.TOP_LEFT;
				
				
				stage.addEventListener(Event.RESIZE,resizeStage);
			}
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO show loader
		}
		
		
		
		
		
		private function resizeStage(evt:Event):void{
			fillBitmapBg();
		}
		
		
		private function fillBitmapBg():void{
			//trace(bitmap)
			var dotBg:Bitmap=new DotBg() as Bitmap;
			var symbol:BitmapData=dotBg.bitmapData;
			
			this.graphics.clear();
			var fillType:String = GradientType.RADIAL;
			var colors:Array = [0x333333 ,0x000000];
			var alphas:Array = [.5, .2];
			var ratios:Array = [0x00, 0xFF];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(stage.stageWidth,stage.stageHeight, 0, 0, 0);
			var spreadMethod:String = SpreadMethod.PAD;
			this.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);  
			this.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			
			graphics.beginBitmapFill(symbol);
			graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			graphics.endFill()
		}
		private function ioError(e:IOErrorEvent):void 
		{
			trace(e.text);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// TODO update loader
			graphics.clear();
			graphics.beginFill(0xff0000);
			if(stage){
				graphics.drawRect(0,stage.stageHeight/2,stage.stageWidth*e.bytesLoaded/e.bytesTotal,1);
			}
			graphics.endFill();
		}
		
		private function checkFrame(e:Event):void 
		{
			if (currentFrame == totalFrames) 
			{
				stop();
				loadingFinished();
			}
		}
		
		private function loadingFinished():void 
		{
			removeEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
			
			// TODO hide loader
			
			startup();
		}
		
		private function startup():void 
		{
			fillBitmapBg();
			
			var mainClass:Class = getDefinitionByName("DCWebSite") as Class;
			addChild(new mainClass() as DisplayObject);
			
		}
		
		
	}
	
}